// pages/index/release.js
var api = require('../../api.js'), app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    // selects: ['date', "select", "text"],
    selects: ['日期选择', "单选复选", "文本内容"],
    select: [{ name: '选项1' }, { name: '选项2' }],
    hb: {
      text: '文本框',
      pic: '图片',
      date: '日期',
      select: '选择框',
    },
    array: ['美国', '中国', '巴西', '日本'],
    index: 0,
    date: '',
    msid: 0,
    zujian: [],
  },
  select: function (e) {
    // console.log(e);
    var index = e.detail.value;
    this.setData({
      index: index
    })
    //console.log(this.data.selects[index]);
  },
  addzujian: function (e) {
    // console.log(this.data.hb);
    var index = this.data.index;
    var selects = this.data.selects;
    var zujian = this.data.zujian;
    var obj = { 'type': selects[index] }
    if (selects[index] == '单选复选') {
      obj = { 'type': selects[index], 'select': [{ name: '选项1' }, { name: '选项2' }], 'name': '选择内容', 'selecttype': '单选' }
    }

    if (selects[index] == '文本内容') {
      obj = { 'type': selects[index], 'name': '文本标题' }
    }

    if (selects[index] == '日期选择') {
      obj = { 'type': selects[index], 'name': '日期标题' }
    }

    if (selects[index] == 'pic') {
      obj = { 'type': selects[index], 'name': '图片' }
    }

    zujian.push(obj);
    console.log(zujian);
    this.setData({
      zujian: zujian,
    })
  },
  shuxinag: function (e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/zujian/zujian?type=' + type + '&index=' + index,
    })
  },
  // 选择具体时间
  bindTimeChange: function (e) {
    console.log(e)
    let that = this
    that.setData({
      time: e.detail.value
    })
  },
  //文本框输入的值
  text: function (e, index) {
    // console.log(e);
    // console.log(index);
    var zujian = this.data.zujian;
    zujian[index].name = e;
    console.log(zujian)
    this.setData({
      zujian: zujian
    })

  },
  //删除 字段
  del: function (e) {
    var zujian = this.data.zujian;
    zujian.splice(e, 1);
    this.setData({
      zujian: zujian
    })
  },
  //是否必填
  isbitian: function (e, index) {
    var zujian = this.data.zujian;
    zujian[index].isbitian = e;
    this.setData({
      zujian: zujian
    })
  },
  isduohang: function (e, index) {
    var zujian = this.data.zujian;
    zujian[index].isduohang = e;
    this.setData({
      zujian: zujian
    })
  },
  updates: function (data, ziduan, index) {
    console.log(data);
    console.log(ziduan);
    console.log(index);
    var zujian = this.data.zujian;
    zujian[index].select = data;
    this.setData({
      zujian: zujian
    })
    // this.setData({
    //   ['' + ziduan + '']: data
    // })
  },
  selecttype: function (type, index) {
    console.log(type);
    console.log(index);
    var zujian = this.data.zujian;
    zujian[index].selecttype = type;
    this.setData({
      zujian: zujian
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (res) {
     var _that = this
     _that.setData({
       options : res
     })
     console.log("onLoad======>",res)
  },
  // 添加
  releaseAdd:function(t){
    wx.reLaunch({
      url: '/pages/address/index?uid='+this.data.options.uid + '&btnTitle='+this.data.options.btnTitle + '&btnText='+ this.data.options.btnText+'&index='+t.currentTarget.dataset.index,
    })
  },
  // 提交
  btn: function(options){
    var options = this.data.options
    var user = wx.getStorageSync("user")
    if(options.deptid == undefined){
       options.deptid = 0
    }else{
      options = this.data.options
    }
    if(options.uids == undefined){
      options.uids = 0
    }
    var _that = this;
    var zujian = JSON.stringify(_that.data.zujian);
    console.log("btn========>", this.data.options,user)
      app.request({
      url: api.infoCenter.create,
      method: "POST",
      data: {
        isadministrator: user.isadministrator,
        uid: options.uid, 
        type: 0,
        publishScope: options.deptid + ',' + options.uids,
        status: 1,
        subject: options.btnTitle,
        content: options.btnText,
        catid: 0,
        attachmentid: 0,
        zujian:zujian
      },
      success: function (res) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        wx.reLaunch({
          url: '/pages/index/index',
          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        })
        console.log("信息添加======>", res)
      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})